import {Exclude} from 'class-transformer';
import {Column, Entity, ManyToMany, OneToOne} from 'typeorm';
import {CommonEntity} from '~/common/entity/common.entity';
import {UserEntity} from '../user.entity';
import { TenantEntity } from '~/modules/tenant/tenant.entity';

@Entity({name: 'sys_user_admin'})
export class UserAdminEntity extends CommonEntity {
  // 用户名
  @Column({unique: true, type: 'varchar', length: 50, comment: '用户名'})
  username: string;

  // 密码
  @Exclude()
  @Column({type: 'varchar', length: 150, comment: '密码'})
  password: string;

  // 密码盐
  @Exclude()
  @Column({name: 'password_salt', type: 'varchar', length: 150, comment: '密码盐'})
  psalt: string;

  // 登录时间
  @Column({name: 'login_time', type: 'datetime', nullable: true, comment: '登录时间'})
  loginTime: Date;

  // 登录IP
  @Column({name: 'login_ip', type: 'varchar', length: 50, nullable: true, comment: '登录IP'})
  loginIp: string;

  // 登录设备
  @Column({name: 'login_device', type: 'varchar', length: 50, nullable: true, comment: '登录设备'})
  loginDevice: string;

  @OneToOne(() => UserEntity, (user) => user.wechat, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
    cascade: ['insert', 'update'],
  })
  user: UserEntity;

	@ManyToMany(() => TenantEntity, (tenant) => tenant.admin)
	tenants: TenantEntity[];
}
